#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ng_my_base.h"

char *arr;

char *strtrim(char *s)
{
  int l = strlen(s);
  arr = (char *) malloc(l + 1);
  memset(arr, 0, l + 1);
  int i=0, j=0;
  for (i = 0; i < l; i++)
  {
    if (s[i] != ' ' && s[i] != '\t')
    {
      arr[j] = s[i];
      j++;
    }
  }
  return arr;
}

char *strrow(char *s)
{
  int l = strlen(s);
  int i = 0;
  if (s[l - 1] == '\n')
  {
    s[l - 1] = '\0';
  }
  return s;
}

/*
db_list = index, ip, port, database, isvalid, user, password, client_flag
db_list = 0:172.17.4.50:3358:oxwf:1:oxwangfeng:oxwangqiang:0
 */
int dblist_init(char *var, char *value, int gid)
{
  char *p;
  p = strtok(value, ":");
  int dblist_sub = atoi(p);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].child_DbInfo_list[dblist_sub].host, p, IP_ADDRESS_LENGTH + 1);
  p = strtok(NULL, ":");
  st_DB_Info[gid].child_DbInfo_list[dblist_sub].port = atoi(p);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].child_DbInfo_list[dblist_sub].child_db_name, p, DB_NAME_LENGTH + 1);
  p = strtok(NULL, ":");
  st_DB_Info[gid].child_DbInfo_list[dblist_sub].used = atoi(p);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].child_DbInfo_list[dblist_sub].user, p, DB_ADMIN_NAME_LENGTH + 1);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].child_DbInfo_list[dblist_sub].pwd, p, DB_ADMIN_PWD_LENGTH + 1);
  p = strtok(NULL, ":");
  st_DB_Info[gid].child_DbInfo_list[dblist_sub].client_flag = atoi(p);
  return 0;
}

/*
table_list = index, table, key_name, key_type, key_order, table_hash_mod
table_list      = 0:shell:id:4:1:1
 */
int tblist_init(char *var, char *value, int gid)
{
  char *p;
  p = strtok(value, ":");
  int tblist_sub = atoi(p);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].table_list[tblist_sub].table_name, p, TABLE_NAME_LENGTH + 1);
  p = strtok(NULL, ":");
  strncpy(st_DB_Info[gid].table_list[tblist_sub].key_name, p, KEY_NAME_LENGTH + 1);
  p = strtok(NULL, ":");
  st_DB_Info[gid].table_list[tblist_sub].key_type = atoi(p);
  p = strtok(NULL, ":");
  st_DB_Info[gid].table_list[tblist_sub].key_col = atoi(p);
  p = strtok(NULL, ":");
  st_DB_Info[gid].table_list[tblist_sub].table_hash_mod = atoi(p);
  return 0;
}

/*
db_name, db_id, db_hash_mode, used
 */
int group_init(char *var, char *value, int gid)
{
  int flag = 0;
  flag = strcmp(var, "db_name");
  if (flag == 0)
  {
    strncpy(st_DB_Info[gid].db_name, value, DB_NAME_LENGTH + 1);
    return 0;
  }

  flag = strcmp(var, "db_id");
  if (flag == 0)
  {
    st_DB_Info[gid].db_id = atoi(value);
    return 0;
  }

  flag = strcmp(var, "db_hash_mod");
  if (flag == 0)
  {
    st_DB_Info[gid].db_hash_mod = atoi(value);
    return 0;
  }

  flag = strcmp(var, "isvalid");
  if (flag == 0)
  {
    st_DB_Info[gid].used = atoi(value);
    return 0;
  }
  return -1;
}

int ng_init(char *var, char *value, int gid)
{

  if (strcmp(var, "db_list") == 0)
  {
    dblist_init(var, value, gid);
    return 0;
  } else if (strcmp(var, "table_list") == 0)
  {
    tblist_init(var, value, gid);
    return 0;
  } else
  {
    group_init(var, value, gid);
    return 0;
  }
  return -1;
}


int group_count = -1;

int read_file(char *s)
{
  int l = 0, rownum = 0;
  char buffer[127], line[127], left[127], right[127];
  FILE *fd = fopen(s, "r");
  memset(buffer, 0, 128);
  memset(line, 0, 128);

  while (fgets(buffer, 128, fd))
  {
    l = strlen(strtrim(strrow(buffer)));
    strncpy(line, arr, 128);
    free(arr);
    if (l == 0 || line[0] == '#')
    {
      continue;
    }

    if (line[0] == '[' && line[l - 1] == ']')
    {
      group_count++;
      printf("group:%d\n", group_count);
      rownum = 0;
    } else
    {
      memset(left, 0, 128);
      strncpy(left, strtrim(strtok(line, "=")), 128);
      free(arr);
      memset(right, 0 , 128);
      strncpy(right, strtrim(strtok(NULL, "=")), 128);
      free(arr);
      printf("ready init:%s=%s\n", left, right);
      if (ng_init(left, right, group_count) != 0)
      {
        printf("ng_init error\n");
        exit(1);
      }
      rownum++;
    }

    memset(line, 0, 128);
  }
  return 0;
}

int main(void)
{
  memset(st_DB_Info, 0, sizeof (st_DB_Info));
  char s[] = "/home/shell/mysql/etc/my_init.conf";
  read_file(s);
  printf("GROUP COUNT:%d\n", ++group_count);
  void my_print();
  my_print();
  return 0;
}

void my_print()
{
  printf("print configurationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxL\n");
  int i = 0;
  int gid = group_count;
  for (i = 0; i <= gid; i++)
  {
    printf("------------------------group info---------------------\n");
    printf("st_DB_Info[%d].db_name=%s\n", gid, st_DB_Info[i].db_name);
    printf("st_DB_Info[%d].used=%d\n", gid, st_DB_Info[i].used);
    printf("st_DB_Info[%d].db_id=%d\n", gid, st_DB_Info[i].db_id);
    printf("st_DB_Info[%d].db_hash_mod = %d\n", gid,  st_DB_Info[i].db_hash_mod);
    printf("\n");
  }
  printf("\n\n---------------------------dblist info-------------------------\n");

  int j = 0;
  for (i = 0; i <= gid; i++)
  {
    for (j = 0; j < 3; j++)
    {
      printf("st_DB_Info[%d].child_DbInfo_list[%d].host=%s\n", i, j, st_DB_Info[i].child_DbInfo_list[j].host);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].port=%d\n", i, j, st_DB_Info[i].child_DbInfo_list[j].port);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].client_flag=%d\n", i, j,  st_DB_Info[i].child_DbInfo_list[j].client_flag);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].used=%d\n", i, j, st_DB_Info[i].child_DbInfo_list[j].used);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].user=%s\n", i, j, st_DB_Info[i].child_DbInfo_list[j].user);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].pwd=%s\n", i, j, st_DB_Info[i].child_DbInfo_list[j].pwd);
      printf("st_DB_Info[%d].child_DbInfo_list[%d].child_db_name=%s\n", i, j, st_DB_Info[i].child_DbInfo_list[j].child_db_name);
    }
  }

  printf("\n\n-------------------------------tblist info------------------------\n");
  for (i = 0; i < gid; i++)
  {
    for (j = 0; j < 3; j++)
    {
      printf("st_DB_Info[%d].table_list[%d].table_name=%s\n", i, j, st_DB_Info[i].table_list[j].table_name);
      printf("st_DB_Info[%d].table_list[%d].key_name=%s\n", i, j, st_DB_Info[i].table_list[j].key_name);
      printf("st_DB_Info[%d].table_list[%d].key_type=%d\n", i, j, st_DB_Info[i].table_list[j].key_type);
      printf("st_DB_Info[%d].table_list[%d].table_hash_mod=%d\n", i, j, st_DB_Info[i].table_list[j].table_hash_mod);
      printf("st_DB_Info[%d].table_list[%d].key_col=%d\n", i, j, st_DB_Info[i].table_list[j].key_col);
      printf("st_DB_Info[%d].table_list[%d].table_hash_mod=%d\n",  i, j, st_DB_Info[i].table_list[j].table_hash_mod);
    }
    printf("\n");
  }
  printf("print configurationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxL\n");
  return ;
}
